package com.Policing.lease;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.Policing.Adapter.Input_Adapter_item;
import com.Policing.BaseActivity;
import com.Policing.Ip_set.Ip_Set;
import com.Policing.R;
import com.Policing.bean.Input_item;
import com.Policing.bean.Issued_bean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hb.dialog.dialog.LoadingDialog;
import okhttp3.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Lease_lv extends BaseActivity {
    private TextView person,money,return_date,time,loan_date;
    private ListView issued_lv;
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();//全部
    LoadingDialog loadingDialog;
    private ImageView fh;
    private List<Input_item> data = new ArrayList<>();
    private String url="http://"+ Ip_Set.IP+"/api/leaseQueryById";
    private String leaseid,status;
    @Override
    public int intiLayout() {
        return R.layout.lease_lv;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadingDialog = new LoadingDialog(this);
        loadingDialog.setMessage("加载中....");
        loadingDialog.show();
    }

    @Override
    public void initView() {
        fh = (ImageView) findViewById(R.id.fh);
        person = (TextView) findViewById(R.id.person);
        money = (TextView) findViewById(R.id.money);
        return_date = (TextView) findViewById(R.id.return_date);
        time = (TextView) findViewById(R.id.time);
        loan_date = (TextView) findViewById(R.id.loan_date);
        issued_lv= (ListView) findViewById(R.id.issued_lv);
    }

    @Override
    public void initData() {
    Intent intent = getIntent();
        person.setText(intent.getStringExtra("person"));
        money.setText(intent.getStringExtra("money"));
        return_date.setText(intent.getStringExtra("return_date"));
        time.setText(intent.getStringExtra("time"));
        loan_date.setText(intent.getStringExtra("loan_date"));
        leaseid = intent.getStringExtra("leaseid");
        status = intent.getStringExtra("status");
        load();
        fh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    private void load() {
        new Thread(){
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    RequestBody requestBody = new FormBody.Builder()
                            .add("leaseId",leaseid)
                            .build();
                    Request request = new Request.Builder()
                            .url(url)
                            .post(requestBody).build();
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    Log.i("222",responseData);
                    showResponse(responseData);//解析
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
    private void showResponse(final String responseData) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                loadingDialog.dismiss();
                if (responseData.equals("[]")){
                    Toast.makeText(Lease_lv.this,"无数据",Toast.LENGTH_SHORT).show();
                    return;
                }
                Bitmap bitmap =null;
                Gson gson = new Gson();
                List<Issued_bean> userList = gson.fromJson(responseData, new TypeToken<List<Issued_bean>>(){}.getType());
                for(int i = 0;i < userList.size();i++){
                    String assetName = userList.get(i).getAssetName();
                    String deptId = userList.get(i).getDeptId();
                    String assetNumber =userList.get(i).getAssetNumber();
                    String userId = userList.get(i).getUserId();
                    String model = userList.get(i).getModel();
                    String type = userList.get(i).getPlace();
                    if (userList.get(i).getImage().equals("")){
                        @SuppressLint("ResourceType") InputStream is = getResources().openRawResource(R.mipmap.null_po);
                        bitmap = BitmapFactory.decodeStream(is);
                    }else {
                        byte [] input = Base64.decode(userList.get(i).getImage(), Base64.DEFAULT);
                        bitmap = BitmapFactory.decodeByteArray(input, 0, input.length);
                    }
                    data.add(new Input_item(bitmap,assetName,deptId,assetNumber,userId,model,type,""));

                }
                Input_Adapter_item adapter_item = new Input_Adapter_item(Lease_lv.this,R.layout.input_adapter_item,data);
                issued_lv.setAdapter(adapter_item);


            }
        });

    }
}
